package controller.shop;

import com.alibaba.fastjson.JSONObject;
import model.Goods;
import model.Order;
import model.OrderGoods;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IOrderGoodsService;
import service.IOrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/8/3
 */
@Controller
@RequestMapping("shop/goods")
public class ShopGoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private IOrderService orderService;

    //用户查询所有上架的商品
    @RequestMapping("queryAllGoods")
    public ModelAndView queryAllAdmin(Goods goods, Integer pageNo) {

        goods.setgStatus(1);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("goods", goods);//保留name值
//        modelAndView.addObject("uId", uId);//保留name值
        Map<String,Object> map = goodsService.queryGoodsByStatus(goods,pageNo);
        modelAndView.addObject("list",  map.get("list"));//将数据传到前端
        modelAndView.addObject("pageNo", map.get("pageNo"));
        modelAndView.addObject("totalPage", map.get("totalPage"));
        modelAndView.addObject("gStatus", goods.getgStatus());
        modelAndView.setViewName("shopping/mall_main2");
        return modelAndView;
    }

    //查看商品详情
    @RequestMapping("GoodsGetails")
    public ModelAndView queryAllAdmin(Goods goods,HttpServletRequest req) {
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        goods.setgStatus(1);
        ModelAndView modelAndView = new ModelAndView();
        List<Goods> list = goodsService.queryAllGoods(goods);
        modelAndView.addObject("goodsInfos", list.get(0));//将数据传到前端
        modelAndView.addObject("uId", user.getId());//将数据传到前端
        modelAndView.setViewName("shopping/mall_product");
        return modelAndView;
    }

    //点击购买进入支付页面
    @RequestMapping("payPage")
    public ModelAndView payPage(Integer carId,Integer gId,Integer buyCount,HttpServletRequest req) {
        //创建session
        HttpSession session = req.getSession();
        //从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        Map<String, Object> map = goodsService.createOrder(carId, gId, buyCount, user.getId());
        ModelAndView modelAndView = new ModelAndView();

        Integer code = (Integer) map.get("code");
        if (code == 1){
            modelAndView.addObject("goodsInfos", map.get("goods"));//将数据传到前端
            modelAndView.addObject("buyCount",buyCount);//将数据传到前端
            modelAndView.addObject("order", map.get("order"));//将数据传到前端
            modelAndView.addObject("uId", user.getId());//将数据传到前端
            modelAndView.setViewName("shopping/mall_pay");
        }else {
            modelAndView.addObject("msg", map.get("msg"));//将数据传到前端
            modelAndView.setViewName("shopping/mall_pay");
        }
        return modelAndView;
    }

    //跳转支付输入密码页面
    @RequestMapping("payPass")
    public ModelAndView payPass(String orderNo,HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orderNo", orderNo);//将数据传到前端
        modelAndView.addObject("uId", user.getId());//将数据传到前端
        modelAndView.setViewName("shopping/mall_pay_pass");
        return modelAndView;
    }

    //验证支付密码，扣款，生成交易流水
    @RequestMapping(value = "pay",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String pay(String uPass,String orderNo,HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        Map<String, Object> map = goodsService.pay(user.getId(), uPass, orderNo);
//        JSONObject json = JSONObject.parseObject(map.toString());
        JSONObject json = new JSONObject();
        json.put("code",map.get("code"));
        json.put("msg",map.get("msg"));
        return json.toJSONString();
    }

    //跳转订单页面
    @RequestMapping("orderPage")
    public ModelAndView orderPage(OrderGoods orderGoods,HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //从session中获取管理员对象
        User user = (User) session.getAttribute("u");
        orderGoods.setuId(user.getId());
        ModelAndView modelAndView = new ModelAndView();
        List<OrderGoods> orderGoods1 = orderGoodsService.queryGoodsToOrder(orderGoods);
        modelAndView.addObject("order",orderGoods);
        modelAndView.addObject("orderGoods",orderGoods1);
        modelAndView.setViewName("shopping/mall_order_list");
        return modelAndView;
    }

    //关闭交易，修改订单的状态值
    @RequestMapping("closePay")
    @ResponseBody
    public String closePay(Order order){
        order.setOrderStatus(7);
        return orderService.updateOrderStatus(order);
    }

    //签收成功  将状态值改为4-已签收
    @RequestMapping("sign")
    @ResponseBody
    public String sign(Order order){
        order.setOrderStatus(4);
        return orderService.updateOrderStatus(order);
    }

    //申请退款 将状态值改为5-退款中
    @RequestMapping("refund")
    @ResponseBody
    public String refund(Order order){
        order.setOrderStatus(5);
        return orderService.updateOrderStatus(order);
    }

    //取消退款   显示原来退款钱的状态值
    @RequestMapping("cancelRefund")
    @ResponseBody
    public String cancelRefund(Order order){
        order.setOrderStatus(5);
        return orderService.cancelOrderStatus(order);
    }
}
